/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.editfile;

import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.mags.remas.logic.EntityFileUtils;
import org.mags.remas.model.entity.EntityFile;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.view.commons.ImageProvider;
import org.mags.remas.control.DocumentInput;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class TextDocumentEditor extends TextEditor {
	public static final String ID = "org.mags.remas.editfile.textDocumentEditor";
	private EntityRelationship entityRelationship;
	private TextDocumentProvider templateDocumentProvider;
	public TextDocumentEditor() {
		super();
		templateDocumentProvider = new TextDocumentProvider();
		setDocumentProvider(templateDocumentProvider);
	}
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		entityRelationship = ((DocumentInput)input).getEntityRelationship();
		loadFile();
		super.init(site, input);
		super.setTitleImage(this.getDefaultImage());
		super.setPartName(entityRelationship.getEntity().getName()+" ["+entityRelationship.getEntity().getCode()+"]");
	}	
	public boolean isEditable() {
		return true;
	}
	@Override
	public Image getDefaultImage() {
		Image image = null;
		if(entityRelationship!=null) 
			image = ImageProvider.getEntityImage(entityRelationship.getEntity().getEntityType().getCode().toLowerCase()+".png");
		if(image==null)
			image = ImageProvider.getCommonImage("edit.png");
		return image;
	}	
	public void createPartControl(Composite parent) {
		GridLayout gridLayout = new GridLayout(1, true);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;		
		parent.setLayout(gridLayout);
		createToolBar(parent);
		Composite composite = new Composite(parent, SWT.BORDER);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		composite.setLayoutData(gridData);
		composite.setLayout(new FillLayout());
		super.createPartControl(composite);
	}
	private void loadFile() {
		List<EntityFile> files = EntityFileUtils.getFiles(entityRelationship.getEntity().getEntityId());
		EntityFile entityFile;
		if(files.size()>0) {
			entityFile = files.get(0);
			String text = new String(entityFile.getRawContent());
			templateDocumentProvider.setContentFile(text);			
		} else {
			entityFile = new EntityFile();
			entityFile.setEntity(entityRelationship.getEntity());
			entityFile.setEntity(entityRelationship.getEntity());
		}
		templateDocumentProvider.setEntityFile(entityFile);		
	}
	public void save() {
		super.performSave(true, null);
	}
	protected void createToolBar(Composite parent) {
		ToolBar toolBar = new ToolBar(parent, SWT.FLAT);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.RIGHT;
		gridData.grabExcessHorizontalSpace = true;		
		toolBar.setLayoutData(gridData);

		ToolItem saveButton = new ToolItem(toolBar, SWT.NONE);
		saveButton.setText("save");
		saveButton.setImage(ImageProvider.getCommonImage("save.png"));
		saveButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				save();
			}
		});		
	}	
}
